/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package metadatabrowser;

import at.fhj.itm.metadata.Procedure;
import at.fhj.itm.metadata.impl.postgres.PostgresProcedure;
import at.fhj.itm.metadata.impl.postgres.PostgresProcedureArgument;
import at.fhj.util.Pair;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.Viewer;

/**
 *
 * @author gerhard
 */
public class ProcedureContentProvider implements IStructuredContentProvider {

    @Override
    public Object[] getElements(Object o) {
        Procedure proc = (Procedure) o;

        List<Pair<String, String>> properties = new ArrayList<Pair<String, String>>();

        properties.add(new Pair<String, String>("Name", proc.getName()));
        properties.add(new Pair<String, String>("Body",proc.getSource()));
        properties.add(new Pair<String, String>("Return type", proc.getReturnType().getName() ));

        if(proc instanceof PostgresProcedure){
            PostgresProcedure pgProc = (PostgresProcedure) proc;
            properties.add(new Pair<String, String>("Language:",pgProc.getLanguage()));
            properties.add(new Pair<String, String>("oid", new Long(pgProc.getOid()).toString()));
            properties.add(new Pair<String, String>("Arguments",
                    createArgumentString(pgProc.getArguments())));
        }


        return properties.toArray();



    }

    private String createArgumentString(List<PostgresProcedureArgument> args){
        StringBuilder sb = new StringBuilder();

        for(PostgresProcedureArgument curArg: args){
            sb.append(String.format("%s %s, ", curArg.getName(), curArg.getType().getName()));
        }


        return sb.toString();
    }
    @Override
    public void dispose() {

    }

    @Override
    public void inputChanged(Viewer viewer, Object o, Object o1) {
    }


}
